package com.zhsq.zlc.controller.customer;

import com.zhsq.zlc.entity.CustomerEntity;
import com.zhsq.zlc.entity.IDEntity;
import com.zhsq.zlc.entity.LogEntity;
import com.zhsq.zlc.entity.ResultObject;
import com.zhsq.zlc.service.customer.CustomerService;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.PageUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CustomerController {
    
        @Autowired
        private CustomerService customerService;

        @Autowired
        private LogService logService;


        @RequestMapping("zhsq/customerManage_{pageCurrent}_{pageSize}_{pageCount}")
        public String CustomerManage(CustomerEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception {
            //判断
            if (pageSize == 0) pageSize = 10;
            if (pageCurrent == 0) pageCurrent = 1;
            int rows = customerService.count(entity);
            if (pageCount == 0) pageCount = rows % pageSize == 0 ? (rows / pageSize) : (rows / pageSize) + 1;

            //查询
            entity.setStart((pageCurrent - 1) * pageSize);
            entity.setEnd(pageSize);
            List<CustomerEntity> list = customerService.list(entity);
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setDataIndex(entity.getStart() + i + 1);
                }
            }
            //输出
            model.addAttribute("list", list);
            String pageHTML = PageUtil.getPageContent("customerManage_{pageCurrent}_{pageSize}_{pageCount}?CustomerName=" + entity.getCustomerName(), pageCurrent, pageSize, pageCount);
            model.addAttribute("pageHTML", pageHTML);
            model.addAttribute("customer", entity);
//            System.out.println(list);
            return "zhsq/customerManage";


        }


        /**
         * 客户新增、修改跳转
         * @param model
         * @param entity
         * @return
         */
        @PostMapping("/zhsq/customerEditGetEntity")
        public String CustomerEditGetEntity(Model model,CustomerEntity entity) throws Exception{
            if(entity.getId()!=null){
                model.addAttribute("customer",customerService.getCustomerById(entity));
            }else{
                model.addAttribute("customer",new CustomerEntity());
            }
            return "zhsq/customerEdit";
        }

        /**
         * 客户新增、修改提交
         * @param model
         * @param entity
         * @param httpSession
         * @return
         */
        @PostMapping("/zhsq/customerEdit")
        public String CustomerEditPost(Model model, CustomerEntity entity, HttpSession httpSession, HttpServletRequest request)throws Exception {
            HttpSession session = request.getSession();
            SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
            if(entity.getId()!=null){
                customerService.edit(entity);
                this.logService.insertLog(LogEntity.EDIT_TYPE, "修改客户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
            } else {
                customerService.add(entity);
                this.logService.insertLog(LogEntity.INSERT_TYPE, "添加客户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
            }
            return "redirect:customerManage_0_0_0";
        }

        /**
         * 删除客户
         * @param entity
         * @return
         */
        @ResponseBody
        @PostMapping("/zhsq/customerDelete")
        public ResultObject<Object> CustomerDelete(IDEntity entity, HttpServletRequest request) throws Exception{
            HttpSession session = request.getSession();
            SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
            customerService.delete(entity.getIntID());
            this.logService.insertLog(LogEntity.DELETE_TYPE, "删除用户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
            ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,null, null);
            return object;
        }
    }


